
//ajax functions
/**
 * fire ajax
 */
function fire(method,url,params,callback){
	var AJAX = new XMLHttpRequest();
	  AJAX.onreadystatechange = callback;
	  AJAX.open(method, url,'true');
	  if(method != "GET")
	  AJAX.send(JSON.stringify(params));
	  else
		  AJAX.send();
	  return AJAX;
	};
	
	/**
	 * checks if the response is valid
	 * @param ajax
	 * @returns
	 */
	function validResponse(ajax) {
		  if(ajax != undefined && ajax.readyState == 4 && ajax.status == 200) {
		      var obj = JSON.parse(ajax.responseText);
		      		ajax.abort();
		      		return obj;
		  }else if(ajax != undefined && ajax.readyState == 4 && ajax.status == 402){
			  unauthorized();
		  }
		}
	

	//adds the time in millis as parameter to the url
	function addTimeToURL(url,startFrom){
		return url += (startFrom == undefined) ? "" : "?startFrom=" + startFrom;
	}
	
	
	function insertParam(url ,key, value){
		if(value == undefined || key == undefined){
			return url;
		}
		
		url = url.split("?");
		var paramsurl = url[1];
		url = url[0];
		return (paramsurl == null) ? fetchParam(url + "?", key, value) : fetchParam(url + "?" + paramsurl + "&", key, value);
		paramsurl = (paramsurl[1] != undefined) ? paramsurl[1] : paramsurl[0];
	}
	
	function fetchParam(url ,key, value){
		key = encodeURI(key);
		value = encodeURI(value);
		return url + key + "=" + value;
	}